import {Node, Animation, SkeletalAnimation, ParticleSystem} from 'cc';

/**
 * 特效工具类
 */
export class EffectUtils {
    /**
     * 禁止创建对象
     */
    private constructor() {
    }

    /**
     * 刷新节点下的动画、粒子播放速度
     *
     * @param targetNode 目标节点
     * @param speed 速度
     */
    public static refreshEffectSpeed(targetNode: Node, speed: number) {
        if (!targetNode) {
            return;
        }
        let arrAni = targetNode.getComponentsInChildren(Animation);
        arrAni.forEach((item: Animation) => {
            item.clips.forEach((clip: any) => {
                let aniName = clip?.name as string
                let aniState = item.getState(aniName);
                aniState.speed = speed;
            })
        })

        let arrSkeletalAni = targetNode.getComponentsInChildren(SkeletalAnimation);
        arrSkeletalAni.forEach((item: SkeletalAnimation) => {
            item.clips.forEach((clip: any) => {
                let aniName = clip?.name as string
                let aniState = item.getState(aniName);
                aniState.speed = speed;
            })
        })

        let arrParticle = targetNode.getComponentsInChildren(ParticleSystem);
        arrParticle.forEach((item: ParticleSystem) => {
            item.simulationSpeed = speed;
        })
    }
}